﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dal
{
    public class JGname
    {
        public JGname(Hotext hotext)
        {
            Hotext = hotext;
        }

        public Hotext Hotext { get; }
        public int xadd(Xiamodel xiamodel)
        {
            Hotext.xiamodels.Add(xiamodel);
            return Hotext.SaveChanges();
        }
        public List<Xiamodel> xshow()
        {
            return Hotext.xiamodels.ToList();
        }
        public int jadd(jgmodel jgmodels)
        {
            var list = Hotext.jgmodels.Count(x => x.Jgname == jgmodels.Jgname && x.isdeleted == false);
            if(list >0)
            {
                return -1;
            }
            Hotext.Add(jgmodels);
            return Hotext.SaveChanges();
        }
        public IQueryable jshow(out int tcount,out int tpage,int pageindex=1,int pagesize=2,string? Jgname="",int? xialaid=null)
        {
            var list = Hotext.jgmodels.Join(Hotext.xiamodels, a => a.xialaid, b => b.Id, (a, b) => new
            {
                a.id,
                a.Jgname,
                a.tu,
                a.xialaid,
                a.yingye,
                a.dizhi,
                a.phone,
                a.steat,
                a.xiangq,
                a.isdeleted,
                b.Xname
            }).Where(x => x.isdeleted == false);
            if(!string.IsNullOrEmpty(Jgname))
            {
                list = list.Where(x => x.Jgname.Contains(Jgname));
            }
            if(xialaid!=null)
            {
                list = list.Where(x => x.xialaid == xialaid);
            }
            tcount = list.Count();
            tpage = (int)Math.Ceiling(tcount * 1.0 / pagesize);
            list = list.OrderBy(x => x.id).Skip((pageindex - 1) * pagesize).Take(pagesize);
            return list;
        }
        public int deleted(string id)
        {
            var shan=id.Split(',');
            foreach(var sh in shan)
            {
                var kiss = Hotext.jgmodels.Find(Convert.ToInt32(sh));
                kiss.isdeleted= true;
            }
            return Hotext.SaveChanges();
        }
        public jgmodel fantian(int id)
        {
            return Hotext.jgmodels.Find(id);
        }
        public int xiugai(jgmodel jgmodels)
        {
            var list = Hotext.jgmodels.Count(x => x.Jgname == jgmodels.Jgname && x.isdeleted == false);
            if (list > 0)
            {
                return -1;
            }
            Hotext.Update(jgmodels);
            return Hotext.SaveChanges();
        }
        public int xiugz(string id,int steat)
        {
            var xiu = id.Split(',');
            foreach (var sh in xiu)
            {
                var xiug = Hotext.jgmodels.Find(Convert.ToInt32(sh));
                xiug.steat = steat;
            }
            return Hotext.SaveChanges();
        }
    }
}
